package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.DTO.GoodsQueryDTO;
import com.example.springboot.controller.DTO.LunQueryDTO;
import com.example.springboot.dao.LunDao;
import com.example.springboot.entity.Goods;
import com.example.springboot.entity.Lun;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/lun")
public class LunController {
    @Autowired
    private LunDao lunDao;

    @GetMapping
    public Result findAll(){
        return Result.success(lunDao.findAll());
    }

    @GetMapping("/page")
    public Result findPage(LunQueryDTO lunQueryDTO){
        if(lunQueryDTO.getCurrentPage()==null||lunQueryDTO.getCurrentPage()<=0||lunQueryDTO.getPageSize()<1){
            return Result.error("参数错误");
        }
        Integer currentPage = lunQueryDTO.getCurrentPage();
        Integer pageSize = lunQueryDTO.getPageSize();

        PageHelper.startPage(currentPage, pageSize);
        List<Lun> lun = lunDao.findByOptions(lunQueryDTO);
        return Result.success(new PageInfo<>(lun));
    }
    @PostMapping
    public Result Save(@RequestBody Lun lun){
        if(lun.getOrginnalName()==null||lun.getStatus()==null){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(lunDao.insert(lun));
    }

    @PutMapping
    public Result Updata(@RequestBody Lun lun){
        if(lun.getId()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
        return Result.success(lunDao.updata(lun));
    }

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
        if(id==null||id==0){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(lunDao.deleteById(id)==1);
    }

}
